<?php

namespace App\Admin\Controller;


use App\Common\Controller\AbstractController;
use App\Common\Logic\CommonLogic;
use App\Server\Model\Nav;
use App\Server\Model\NavGroup;
use Hyperf\Di\Annotation\Inject;
use App\Admin\Validate\NavValidate;

class NavController extends AbstractController
{

    /**
     * @Inject()
     * @var NavValidate
     */
    protected $NavValidate;

    public function index()
    {
        $page = $this->request->query('page');
        if ($page) {
            $param = $this->request->all();
            $navGroup = new NavGroup();
            $list = $navGroup->getListPage([], ['*'], null, null,(int)$param['limit']);
            return $this->success($list['data'], '成功', ['count'=>$list['total']]);
        }
        else
        {
            return $this->render->render('/admin/nav/index');
        }
    }

    public function navIndex()
    {
        $param = $this->request->all();
        $page = $this->request->query('page');
        if ($page) {
            $where = [];
            if(empty($param['nav_group_id']))
            {
                return $this->error('请求错误');
            }
            $where[] = ['nav_group_id','=',$param['nav_group_id']];
            $navModel = new Nav();
            $list = $navModel->getList($where, ['*'], 'sort desc');
            $list = (new CommonLogic())->taglist($list);
            foreach ($list as &$value){
                $value['title'] = $value['str'].$value['title'];
            }
            return $this->success($list, '成功');
        }
        else
        {
            $data = [];
            $data['nav_group_id'] = $param['nav_group_id'];
            return $this->render->render('/admin/nav/nav_index',$data);
        }
    }


    public function addGroup()
    {
        $page = $this->request->query('page');
        if ($page) {
            $param = $this->request->all();
            unset($param['page']);
            $errorMessage = $this->NavValidate->addGroup($this->request);
            //验证部分数据合法性
            if ($errorMessage) {
                return $this->error('提交失败：' . $errorMessage);
            }

            $navGroup = new NavGroup();
            $addRes = $navGroup->dataAdd($param);
            if ($addRes){
                return $this->success([],'创建成功');
            }else{
                return $this->error('创建失败');
            }
        }
        else 
        {
            return $this->render->render('/admin/nav/add_group');
        }
    }

    public function addNav()
    {
        $param = $this->request->all();
        $page = $this->request->query('page');
        if ($page) {
            unset($param['page']);
            $errorMessage = $this->NavValidate->addNav($this->request);
            //验证部分数据合法性
            if ($errorMessage) {
                return $this->error('提交失败：' . $errorMessage);
            }
            unset($param['file']);
            $navModel = new Nav();
            $addRes = $navModel->dataAdd($param);
            if ($addRes){
                return $this->success([],'创建成功');
            }else{
                return $this->error('创建失败');
            }
        }
        else
        {
            $data = [];
            $data['nav_group_id'] = $param['nav_group_id'];
            $navModel = new Nav();
            $list = $navModel->getList([['nav_group_id','=',$param['nav_group_id']]], ['*'], 'sort desc');
            $data['navs'] = (new CommonLogic())->taglist($list);
            return $this->render->render('/admin/nav/add_nav',$data);
        }
    }


    public function editGroup()
    {
        $id = $this->request->query('id');
        $navGroup = new NavGroup();
        $page = $this->request->query('page');
        if ($page) {
            $param = $this->request->all();
            unset($param['page']);
            $errorMessage = $this->NavValidate->editGroup($this->request);
            //验证部分数据合法性
            if ($errorMessage) {
                return $this->error('提交失败：' . $errorMessage);
            }

            $navGroup = new NavGroup();
            $updateRes = $navGroup->dataUpdate([['id','=',$param['id']]],$param);
            if ($updateRes){
                return $this->success([],'修改成功');
            }else{
                return $this->error('修改失败');
            }
        } 
        else
        {
            $data = [];
            $data['data'] = $navGroup -> getInfo([['id','=',$id]],['*']);
            return $this->render->render('/admin/nav/edit_group', $data);
        }
    }

    public function editNav()
    {
        $navModel = new Nav();
        $id = $this->request->query('id');
        $page = $this->request->query('page');
        $param = $this->request->all();
        if ($page) {
            unset($param['page']);
            $errorMessage = $this->NavValidate->editNav($this->request);
            //验证部分数据合法性
            if ($errorMessage) {
                return $this->error('提交失败：' . $errorMessage);
            }
            unset($param['file']);
            $updateRes = $navModel->dataUpdate([['id','=',$param['id']]],$param);
            if ($updateRes){
                return $this->success([],'修改成功');
            }else{
                return $this->error('修改失败');
            }
        }
        else
        {
            $data = [];
            $data['data'] = $navModel->getInfo([['id','=',$id]],['*']);
            $navModel = new Nav();
            $list = $navModel->getList([['nav_group_id','=',$param['nav_group_id']]], ['*'], 'sort desc');
            $data['navs'] = (new CommonLogic())->taglist($list);
            return $this->render->render('/admin/nav/edit_nav', $data);
        }
    }


    public function deleteGroup()
    {
        $nav = new Nav();
        $navGroup = new NavGroup();
        $id = $this->request->query('id');
        if(!$id) {
            return $this->error('缺少必要条件');
        }
        $count = $nav->dataCount([["nav_group_id" ,'=', $id]]);
        if ($count > 0) {
            return $this->error('该分组下还存在导航，请先删除导航数据');
        }
        $deleteRes = $navGroup->dataDelete([['id','=',$id]]);
        if (!$deleteRes) {
            return $this->error('删除失败！');
        }
        return $this->success([],'删除成功！');
    }


    public function deleteNav()
    {
        $id = $this->request->query('id');
        if(!$id) {
            return $this->error('缺少必要条件');
        }
        $nav = new Nav();
        $deleteRes = $nav->dataDelete([['id','=',$id]]);
        if (!$deleteRes) {
            return $this->error('删除失败！');
        }
        return $this->success([],'删除成功！');
    }
}
